const fp = require("lodash/fp")
const { Maybe, Container } = require("./support")

// 1、fp.add(x, y)、fp.map(f, x) 创建一个能让functor里的值增加的函数ex1
let maybe = Maybe.of([5, 6, 1])
let ex1 = () => {
  return maybe.map(fp.map(x => fp.add(x, 1)))
}
console.log(ex1())


// 2、实现函数ex2，能够使用fp.first获取列表第一个元素
let xs = Container.of(['do', 'ray', 'me', 'fa', 'so', 'la', 'ti', 'do'])
let ex2 = () => {
  return xs.map(fp.first)
}
console.log(ex2())


// 3、实现函数ex3，使用 safeProp 和 fp.first 找到user的名字的首字母
let safeProp = fp.curry(function(x, o) {
  return Maybe.of(o[x])
})
let user = { id: 2, name: "Albert"}
let ex3 = () => {
  return safeProp("name", user).map(fp.first)
}
console.log(ex3());


// 4、使用 Maybe 重写ex4 不要有if
let ex4 = function(n) {
  // if (n) {
  //   return parseInt(n)
  // }
  return Maybe.of(n).map(x => parseInt(x))
}
console.log(ex4("321"))